<?php

namespace app\common\model\hotel;



/**
 *
 */
class Hotel
{
    //渠道名称:
    //旅小二Pro
    //open_key:
    //vdd6ryjbmqa4
    //open_secret:
    //7177afb18410722d99e424defe4f9dbb
    //
    //PMS测试环境：
    //地址：https://release-pms.napbee.cn/homePage
    //账号：13633338888
    //密码：123456
    //测试环境门店信息调用
    //
    //云掌柜门店ID：64002
    //开放平台酒店ID：57535
    //门店名称：【测试】上海门店
    public $open_key = 'vdd6ryjbmqa4';

    public $open_secret = '7177afb18410722d99e424defe4f9dbb';

    public $url = 'https://open-partner.miot.com.cn/api';

    //获取access-token
    public function get_token()
    {
        $url = $this->url.'/v1/get_token';

        $param = [
            'open_key'=>$this->open_key,
            'open_secret'=>$this->open_secret,
        ];

        $data = json_encode($param,320);

        $res = https_request($url,$data,'');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data']['access_token'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //获取酒店列表
    public function get_hotel_list($page,$page_size)
    {
        $url = $this->url.'/v1/channel/get_hotel_list';

        $param = [
            'page'=>$page,
            'page_size'=>$page_size
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //获取酒店详情
    public function get_hotel_detail($hotel_id)
    {
        $url = $this->url.'/v1/channel/get_hotel_detail';

        $param = [
            'hotel_id'=>$hotel_id,
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //获取房型列表
    public function room_type_list($hotel_id,$page,$page_size)
    {
        $url = $this->url.'/v1/channel/room_type_list';

        $param = [
            'hotel_id'=>$hotel_id,
            'page'=>$page,
            'page_size'=>$page_size
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //获取房型详情
    public function room_type_detail($hotel_id,$room_type_code)
    {
        $url = $this->url.'/v1/channel/room_type_detail';

        $param = [
            'hotel_id'=>$hotel_id,
            'room_type_code'=>$room_type_code,
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }


    //获取房型价格列表
    public function room_price_list($hotel_id,$room_type_code,$checkin_date,$checkout_date)
    {
        $url = $this->url.'/v1/channel/room_price_list';

        $param = [
            'hotel_id'=>$hotel_id,
            'room_type_code'=>$room_type_code,
            'checkin_date'=>$checkin_date,
            'checkout_date'=>$checkout_date
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }


    //查询酒店房间可订
    public function get_room_status($hotel_id,$room_code_list,$start_date,$end_date)
    {
        $url = $this->url.'/v1/channel/get_room_status';
        $room_code_list = json_decode($room_code_list,true)?json_decode($room_code_list,true):_json_decode($room_code_list);
        $param = [
            'hotel_id'=>$hotel_id,
            'room_code_list'=>$room_code_list,
            'start_date'=>$start_date,
            'end_date'=>$end_date
        ];
        //dump($room_code_list);
        //dump($param);die;
        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //查询酒店房型可订数量
    public function get_room_type_stock($hotel_id,$room_type_code_list,$checkin_date,$checkout_date)
    {
        $url = $this->url.'/v1/channel/get_room_type_stock';
        $room_type_code_list = json_decode($room_type_code_list,true)?json_decode($room_type_code_list,true):_json_decode($room_type_code_list);
        $param = [
            'hotel_id'=>$hotel_id,
            'room_type_code_list'=>$room_type_code_list,
            'checkin_date'=>$checkin_date,
            'checkout_date'=>$checkout_date
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'POST');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //查询酒店房型下可订房间
    public function get_room_type_clear_room_list($hotel_id,$room_type_code,$checkin_date,$checkout_date)
    {
        $url = $this->url.'/v1/channel/new/get_room_type_clear_room_list';

        $param = [
            'hotel_id'=>$hotel_id,
            'room_type_code'=>$room_type_code,
            'checkin_date'=>$checkin_date,
            'checkout_date'=>$checkout_date
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    //查询酒店单日房态
    public function get_room_order_state($hotel_id,$date)
    {
        $url = $this->url.'/v1/channel/order/get_room_order_state';

        $param = [
            'hotel_id'=>$hotel_id,
            'date'=>$date,
        ];

        $token = $this->get_token();

        $data = json_encode($param,320);

        $res = https_request($url,$data,$token,'GET');

        $result = json_decode($res,true);

        if ($result['code']==200){
            return $result['data'];
        } else {
            throw new \Exception($result['message']);
        }
    }

}